package com.zx.cloud.controller;


import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zx.cloud.ApiVersion;
import com.zx.cloud.RequestLogging;
import com.zx.cloud.Shift;
import com.zx.cloud.common.filter.CurrentUserId;
import com.zx.cloud.domain.RepayOrder;
import com.zx.cloud.domain.User;
import com.zx.cloud.model.response.ObjectDataResponse;
import com.zx.cloud.service.LogService;
import com.zx.cloud.service.RepayOrderService;
import com.zx.cloud.service.UserService;
import com.zx.cloud.util.Page;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 * @ClassName: RepayOrderController
 * @Description: 贷后管理
 * @author: likun
 * @date: 2017年10月30日 下午5:24:51
 */
@RestController
@RequestMapping(value = ApiVersion.PROVIDER_V1_PREFIX+"/repay")
public class RepayOrderController{

	private static final Logger logger=Logger.getLogger(RepayOrderController.class);
	@Autowired
	private RepayOrderService repayOrderService;
	@Autowired
	private LogService  logService; 
	@Autowired
	private UserService   userService;
	
	@RequestLogging
	@ApiOperation(value = "贷后管理-还款订单列表", notes = "贷后管理-还款订单列表")
	@RequestMapping(value = "/getRepayOrderList", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<RepayOrder>> getRepayOrderList(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody RepayOrder repayOrder){
		User  user=userService.get(userId);
		repayOrder.setVerifyCode(user.getVerifyCode());
		
		Page<RepayOrder> repayOrderList=repayOrderService.pageRepayOrder(repayOrder);
		logService.addLog(userId, "查询还款订单列表", 0);
		return new ObjectDataResponse<Page<RepayOrder>>(repayOrderList);
	}
	
	@RequestLogging
	@ApiOperation(value = "贷后管理-还款订单列表-查看详情", notes = "贷后管理-还款订单列表-查看详情")
	@RequestMapping(value = "/getRepayOrderDetail", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<RepayOrder> getRepayOrderDetail(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody RepayOrder repayOrder){
		RepayOrder repayOrderData=repayOrderService.getRepayOrderDetail(repayOrder);
		logService.addLog(userId, "查询还款订单详情", 0);
		return new ObjectDataResponse<RepayOrder>(repayOrderData);
	}
	@RequestLogging
	@ApiOperation(value = "贷后管理-还款订单列表-还款备注", notes = "贷后管理-还款订单列表-还款备注")
	@RequestMapping(value = "/repayrRemark", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> repayrRemark(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody RepayOrder repayOrder){
		if(userId==null){
			 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"未登录");
		}
		Long id=repayOrder.getId();
		if(id==null){
			 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[id]还款订单主键为空啦");
		}
		int   success= repayOrderService.repayrRemark(repayOrder,userId);
		logService.addLog(userId, "还款订单列表增加还款备注", 0);
		return new ObjectDataResponse<Integer>(success);
	}
	@RequestLogging
	@ApiOperation(value = "贷后管理-还款订单列表-还款短信", notes = "贷后管理-还款订单列表-还款短信")
	@RequestMapping(value = "/repaySendSMS", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> repaySendSMS(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody RepayOrder repayOrder){
		if(userId==null){
			 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"未登录");
		}
		Long id=repayOrder.getId();
		if(id==null){
			 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[id]还款订单主键为空啦");
		}
		int   success= repayOrderService.repaySendSMS(repayOrder,userId);
		logService.addLog(userId, "还款订单列表还款短信", 0);
		return new ObjectDataResponse<Integer>(success);
	}
	@RequestLogging
	@ApiOperation(value = "贷后管理-逾期订单列表", notes = "贷后管理-逾期订单列表")
	@RequestMapping(value = "/getOverdueOrderList", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<RepayOrder>> getOverdueOrderList(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody RepayOrder repayOrder){
		Page<RepayOrder> overdueOrderList=repayOrderService.pageOverdueOrder(repayOrder);
		logService.addLog(userId, "贷后管理-逾期订单列表", 0);
		return new ObjectDataResponse<Page<RepayOrder>>(overdueOrderList);
	}
	@RequestLogging
	@ApiOperation(value = "手动生成逾期利息", notes = "手动生成逾期利息")
	@RequestMapping(value = "/handBuildOverdue", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> handBuildOverdue(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId){
		int   success= repayOrderService.buildOverdue();
		logService.addLog(userId, "手动生成逾期利息", 0);
		return new ObjectDataResponse<Integer>(success);
	}
	@RequestLogging
	@ApiOperation(value = "手动代扣", notes = "手动代扣")
	@RequestMapping(value = "/handWithhold", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> handWithhold(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId){
		int   success= repayOrderService.withhold();
		logService.addLog(userId, "手动代扣", 0);
		return new ObjectDataResponse<Integer>(success);
	}
	
	@RequestLogging
	@ApiOperation(value = "我的还款记录", notes = "我的还款记录")
	@RequestMapping(value = "/getMyRepayOrderList", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<RepayOrder>> getMyRepayOrderList(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody RepayOrder repayOrder){
		Long clientId=repayOrder.getClientId();
		if(clientId==null){
			 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[clientId]为空啦");
		}
		Page<RepayOrder> repayOrderList=repayOrderService.pageMyRepayOrder(repayOrder);
		return new ObjectDataResponse<Page<RepayOrder>>(repayOrderList);
	}
	
	@RequestLogging
	@ApiOperation(value = "立即还款保存交易信息", notes = "立即还款保存交易信息")
	@RequestMapping(value = "/saveTradeInfoByRepay", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> saveTradeInfoByRepay(@RequestBody RepayOrder repayOrder){
		Long clientId=repayOrder.getClientId();
		if(clientId==null){
			 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[clientId]为空啦");
		}
		String tradeNum=repayOrder.getTradeNumber();
		if(StringUtils.isEmpty(tradeNum)){
			 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[tradeNumber]还款订单 申请交易流水号为空啦");
		}
		int   success= repayOrderService.updateRepayDetailByRepayment(repayOrder);
		return new ObjectDataResponse<Integer>(success);
	}
	
	@RequestLogging
	@ApiOperation(value = "立即还款获取申请交易流水号", notes = "立即还款保存交易信息")
	@RequestMapping(value = "/getTradeNum", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<String> getTradeNum(@RequestBody RepayOrder repayOrder){
		Long clientId=repayOrder.getClientId();
		if(clientId==null){
			 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[clientId]为空啦");
		}
		String orderNo=repayOrder.getOrderNo();
		if(StringUtils.isEmpty(orderNo)){
			 Shift.fatal(StatusCode.INVALID_MODEL_FIELDS,"[orderNo]还款订单编号为空啦");
		}
		String   success= repayOrderService.createRepayDetailByRepayment(repayOrder);
		return new ObjectDataResponse<String>(success);
	}
	
}
